package com.lzs.collection.list.traverse.exercise;

import lombok.Data;

import java.awt.print.Book;
import java.util.*;

public class ListExercise {
    @SuppressWarnings("all")
    public static void main(String[] args) {
        List list = new LinkedList();
        List list1 = new ArrayList();
        List list2 = new Vector();
        list.add(new Book("红楼梦", "曹雪芹", 100));
        list.add(new Book("西游记", "吴承恩", 150));
        list.add(new Book("水浒传", "施耐庵", 80));
        list.add(new Book("三国", "罗贯中", 90));
        sort(list);
        for (Object o : list) {
            System.out.println(o);
        }
        System.out.println("======");
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            Object next = iterator.next();
            System.out.println(next);
        }
        System.out.println("======");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }


    }

    @SuppressWarnings("all")
    public static void sort(List list) {
        int size = list.size();
        for (int i = 0; i < size - 1; i++) {
            for (int j = 0; j < size - i - 1; j++) {
                Book book1 = (Book) list.get(j);
                Book book2 = (Book) list.get(j + 1);
                if (book1.getPrice() > book2.getPrice()) {
                    list.set(j, book2);
                    list.set(j + 1, book1);
                }
            }
        }
    }

    @Data
    static class Book {
        private String name;
        private String author;
        private int price;

        public Book(String name, String author, int price) {
            this.name = name;
            this.author = author;
            this.price = price;
        }
    }
}
